package com.ruoyi.business.service.impl;

import java.util.List;

import com.ruoyi.business.tool.CustomerInfoTool;
import com.ruoyi.business.tool.UserInfoTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.business.mapper.AcCustomerInfoMapper;
import com.ruoyi.business.domain.AcCustomerInfo;
import com.ruoyi.business.service.IAcCustomerInfoService;

/**
 * 客户台账管理Service业务层处理
 *
 * @author ruoyi
 * @date 2025-01-20
 */
@Service
public class AcCustomerInfoServiceImpl implements IAcCustomerInfoService
{
    @Autowired
    private AcCustomerInfoMapper acCustomerInfoMapper;

    @Autowired
    private CustomerInfoTool customerInfoTool;

    @Autowired
    private UserInfoTool userInfoTool;

    /**
     * 查询客户台账管理
     *
     * @param customerId 客户台账管理主键
     * @return 客户台账管理
     */
    @Override
    public AcCustomerInfo selectAcCustomerInfoByCustomerId(Long customerId)
    {
        return acCustomerInfoMapper.selectAcCustomerInfoByCustomerId(customerId);
    }


    @Override
    public AcCustomerInfo selectAcCustomerInfoByIdCard(String  idCard)
    {
        return acCustomerInfoMapper.selectAcCustomerInfoByIdCard(idCard);
    }


    /**
     * 查询客户台账管理列表
     *
     * @param acCustomerInfo 客户台账管理
     * @return 客户台账管理
     */
    @Override
    public List<AcCustomerInfo> selectAcCustomerInfoList(AcCustomerInfo acCustomerInfo)
    {
        acCustomerInfo.setDeptId(userInfoTool.getUserDeptId());
        return acCustomerInfoMapper.selectAcCustomerInfoList(acCustomerInfo);
    }

    /**
     * 新增客户台账管理
     *
     * @param acCustomerInfo 客户台账管理
     * @return 结果
     */
    @Override
    public int insertAcCustomerInfo(AcCustomerInfo acCustomerInfo)
    {
        if("1".equals(acCustomerInfo.getCustomerType())){
            //先根据身份证查询一下，如果存在就返回，不存在就新增
            AcCustomerInfo info = acCustomerInfoMapper.selectAcCustomerInfoByIdCard(acCustomerInfo.getIdCard());
        if (info != null){
            throw new RuntimeException("身份证号已存在");
        }else {
            return acCustomerInfoMapper.insertAcCustomerInfo(acCustomerInfo);
        }
            }

        return acCustomerInfoMapper.insertAcCustomerInfo(acCustomerInfo);
    }

    /**
     * 修改客户台账管理
     *
     * @param acCustomerInfo 客户台账管理
     * @return 结果
     */
    @Override
    public int updateAcCustomerInfo(AcCustomerInfo acCustomerInfo)
    {
        acCustomerInfo.setUpdateBy(userInfoTool.getUserName());
        acCustomerInfo.setUpdateTime(userInfoTool.getCurrentDate());

        if("1".equals(acCustomerInfo.getCustomerType())){
            //先根据身份证查询一下，如果存在就返回，不存在就新增
            AcCustomerInfo info = acCustomerInfoMapper.selectAcCustomerInfoByIdCard(acCustomerInfo.getIdCard());
            if (info != null){
                throw new RuntimeException("身份证号已存在");
            }else {
                return acCustomerInfoMapper.updateAcCustomerInfo(acCustomerInfo);
            }
        }
        return acCustomerInfoMapper.updateAcCustomerInfo(acCustomerInfo);
    }

    /**
     * 批量删除客户台账管理
     *
     * @param customerIds 需要删除的客户台账管理主键
     * @return 结果
     */
    @Override
    public int deleteAcCustomerInfoByCustomerIds(Long[] customerIds)
    {
        return acCustomerInfoMapper.deleteAcCustomerInfoByCustomerIds(customerIds);
    }

    /**
     * 删除客户台账管理信息
     *
     * @param customerId 客户台账管理主键
     * @return 结果
     */
    @Override
    public int deleteAcCustomerInfoByCustomerId(Long customerId)
    {
        return acCustomerInfoMapper.deleteAcCustomerInfoByCustomerId(customerId);
    }

    // ****************************************************************************************************************

    @Override
    public int checkAndInsertAcCustomerInfo(AcCustomerInfo acCustomerInfo) {
        String customerCode = customerInfoTool.generateRandomNumber().toString();
        switch (acCustomerInfo.getCustomerType()) {  // 判断客户类型，生成对应客户编号
            case "0":
                customerCode = "B" + customerCode;
                break;
            case "1":
                customerCode = "C" + customerCode;
                break;
        }
        acCustomerInfo.setCustomerCode(customerCode);  // 初始化客户编号
        acCustomerInfo.setCustomerLeaseCount(0L);  // 初始化客户租赁次数状态
        acCustomerInfo.setCustomerLeaseStatus("1");  // 初始化客户租赁，状态0：在租，1：未租
        acCustomerInfo.setCustomerIsDelete(0);  // 初始化客户删除状态，0：正常，1：删除
        acCustomerInfo.setDeptId(userInfoTool.getUserDeptId());
        acCustomerInfo.setCreateBy(userInfoTool.getUserName());
        acCustomerInfo.setCreateTime(userInfoTool.getCurrentDate());
        return this.insertAcCustomerInfo(acCustomerInfo);
    }

    @Override
    public int checkAndDeleteAcCustomerInfo(Long[] customerIds) {
        for(Long customerInfoId : customerIds) {  // 去匹配每个id对应的客户租赁状态是否为0:在租，如果为在租状态不可以删除，如果1:未租，可以删除
            AcCustomerInfo info = acCustomerInfoMapper.selectAcCustomerInfoByCustomerId(customerInfoId);
            if(info.getCustomerLeaseStatus().equals("0")) {
                return 0;
            }
        }
        return this.deleteAcCustomerInfoByCustomerIds(customerIds);
    }


    /**
     * 客户状态定时器
     * 判断该客户租赁信息中，租赁状态是否有在租
     * 若该客户没有在租信息，则将该客户租赁状态改为1：未租
     * **/
    @Override
    public void updateAcCustomerInfoByLeaseStatus() {
        List<AcCustomerInfo> acCustomerInfoList = this.selectAcCustomerInfoList(null);  // 返回所有客户信息
        for(AcCustomerInfo acCustomerInfo : acCustomerInfoList) {  // 遍历所有客户信息
            if(acCustomerInfo.getCustomerLeaseStatus().equals("0")) {
                /*
                * 首先判断客户状态
                * 如果在租，则进行下一步操作
                * 如果未租，则跳过
                * */
                Long customerId = acCustomerInfo.getCustomerId();  // 每位客户id
                int count = acCustomerInfoMapper.selectAcCustomerCountByCustomerId(customerId);  // 查询该客户是否有未到期的租赁信息
                if(0 == count) {  // 如果0 == count，则该客户所有租赁都到期
                  AcCustomerInfo updateAcCustomerInfo = new AcCustomerInfo();
                    updateAcCustomerInfo.setCustomerId(customerId);
                    updateAcCustomerInfo.setCustomerLeaseStatus("1");
                    acCustomerInfoMapper.updateAcCustomerInfo(updateAcCustomerInfo);
                }
            }

        }
    }

    @Override
    public List<AcCustomerInfo> selectCustomerNum() {
        return acCustomerInfoMapper.selectCustomerNum();
    }

    @Override
    public List<AcCustomerInfo> selectCustomerType() {
        return  acCustomerInfoMapper.selectCustomerType();
    }

    @Override
    public List<AcCustomerInfo> selectCustomerStatus() {
        return acCustomerInfoMapper.selectCustomerStatus();
    }

    @Override
    public List<AcCustomerInfo> selectCustomerTop5() {
        return acCustomerInfoMapper.selectCustomerTop5();
    }
}
